//
//  LoginViewController.m
//  田园
//
//  Created by 范明星 on 16/4/16.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "LoginViewController.h"
#import "userManager.h"
#import <AFNetworking/AFNetworking.h>
#import "RegistViewController.h"
#import "SpecialViewController.h"
@interface LoginViewController ()
@property (strong, nonatomic) IBOutlet UITextField *name;
@property (strong, nonatomic) IBOutlet UITextField *password;

@end

@implementation LoginViewController






//登陆数据请求
- (void)requestData {
 
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:REQUESTLOGIN parameters:@{@"action":@"login", @"password":self.password.text,  @"user":self.name.text} progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSLog(@"aaa%@",dic);
        dispatch_async(dispatch_get_main_queue(), ^{
            if ([dic[@"status"] intValue] == 0) {
                NSString *msg = dic[@"msg"];
                NSLog(@"%@", msg);
                UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"登陆失败" message:msg preferredStyle:UIAlertControllerStyleAlert];
                [alertVC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    [alertVC dismissViewControllerAnimated:YES completion:nil];
                }]];
                [self presentViewController:alertVC animated:YES completion:nil];
            }else{
                NSLog(@"登陆成功");
                [userManager saveuserID:dic[@"result"][@"id"] ];
                //                [userManager saveserMobile:dic[@"result"][@"mobile"]];
                [userManager savePassWord:dic[@"result"][@"password"]];
                
                //登陆成功跳转到主页面
                SpecialViewController *speVC = [[SpecialViewController alloc] init];
                
                UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:speVC];
                UITabBarController *tabVC = [[UITabBarController alloc] init];
                tabVC.viewControllers = @[naVC];
                [self presentViewController:tabVC animated:YES completion:nil];
            }
        });

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
 
}
//登陆按钮
- (IBAction)Login:(id)sender {
    [self  requestData];
}

//跳转注册界面
- (IBAction)EnterRegist:(id)sender {
    RegistViewController *registVC = [[RegistViewController alloc] init];
    [self presentViewController:registVC animated:YES completion:nil];
}


- (void)changeLoginTextFiledColor
{
    //NSAttributedString带有属性的文字，（富文本技术）；

    NSMutableAttributedString *PlaceHoderString = [[NSMutableAttributedString alloc] initWithString:@"手机号"];
    [PlaceHoderString setAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]} range:NSMakeRange(0, 3)];
    self.PhoneNumberTextFiled.attributedPlaceholder = PlaceHoderString;
}




- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // Do any additional setup after loading the view from its nib.
}


//让当前控制器的状态栏为白色
- (UIStatusBarStyle) preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
    
   
}









- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
